<?php
/**
 * Created by PhpStorm.
 * Author: DaXiong
 * Date: 2018/10/26
 * Time: 14:06
 */

namespace App\Http\Controllers\Admin;


use App\Http\Controllers\Controller;
use App\Models\Comments;
use App\Models\Praises;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class CommentController extends Controller
{
    /**
     * 获取 评论管理_列表
     * @return \Illuminate\Http\JsonResponse
     * Author: DaXiong
     */
    public function index(){
        try{
            $params = [
                "video_id" => (int)request('video_id',0),
                "content" => (string)request('content',''),
            ];
            $list = Comments::where(function($query) use($params){
                if ($params['video_id'] > 0){
                    $query->where('video_id',$params['video_id']);
                }
                if ($params['content'] !== ''){
                    $query->where('content','like','%'.$params['content'].'%');
                }
                })
                ->with(['user','video'])
                ->orderBy('id','desc')
                ->paginate(30);

            return $this->returnJson(0, '成功获取评论列表', $list);
        }catch (\Exception $exception){
            Log::error($exception->getMessage());
            return $this->returnJson(1);
        }
    }

    /**
     * 评论管理_新增
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * Author: DaXiong
     */
    public function add(Request $request)
    {
        $data = $request->all();
        $data['userid'] = (int)request()->user()->id;

        $data['video_id'] = (int)request('video_id','');
        $data['content'] = (string)request('content','');
        $data['replay_id'] = (int)request('replay_id','');
        $data['praise'] = (int)request('praise','');

        if($data['video_id'] == 0){
            return $this->returnJson(1, '评论失败：未指定视频ID');
        }
        if($data['content'] == ""){
            return $this->returnJson(1, '评论失败：评论内容不能为空');
        }
        try {
            Comments::create($data);
            return $this->returnJson(0, '评论成功');
        } catch (RequestException $exception) {
            Log::error($exception);
            return $this->returnJson(1, '评论失败');
        }
    }

    /**
     * 评论管理_编辑
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * Author: DaXiong
     */
    public function edit(Request $request){
        $data = $request->all();

        $data['userid'] = (int)request()->user()->id;

        $id = (int)request('id','');
        $data['video_id'] = (int)request('video_id','');
        $data['content'] = (string)request('content','');
        $data['replay_id'] = (int)request('replay_id','');
        $data['praise'] = (int)request('praise','');
        if($data['id'] == 0){
            return $this->returnJson(1, '评论修改失败：未指定评论ID');
        }
        if($data['video_id'] == 0){
            return $this->returnJson(1, '评论修改失败：未指定视频ID');
        }
        if($data['content'] == ""){
            return $this->returnJson(1, '评论修改失败：评论内容不能为空');
        }
        try {
            Comments::where('id',$id)->update($data);
            return $this->returnJson(0, '评论更新成功');
        } catch (RequestException $exception) {
            Log::error($exception);
            return $this->returnJson(1, '评论更新失败');
        }
    }

    /**
     * 删除评论
     * @return \Illuminate\Http\JsonResponse
     * Author: DaXiong
     */
    public function destroy(){
        $comment_id = (int)request('id','');
        DB::beginTransaction();	//开启事务
        try {
            Comments::where('id', $comment_id)->delete();
            Praises::where('comment_id',$comment_id)->delete(); //删除点赞
            DB::commit();
            return $this->returnJson(0, '删除成功');
        } catch (RequestException $exception) {
            Log::error($exception->getMessage());
            DB::rollback();
            return $this->returnJson(1, '删除失败');
        }
    }

    /**
     * 批量删除评论
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroyAll(){
        $lists = request('data','');
        $array = array();
        $json = array();
        if($lists){
            DB::beginTransaction();	//开启事务
            foreach ($lists as $key =>$value){
                $json = json_decode($value,true);
                $array[$key] = $json['id'];
                Praises::where('comment_id',$json['id'])->delete(); //删除点赞
            }
//            return $array;
            try {
                Comments::destroy($array);
                DB::commit();
                return $this->returnJson(0);
            } catch (\Exception $e) {
                Log::error($e->getMessage());
                DB::rollback();
                return $this->returnJson(1, '删除失败');
            }
        }
        return $this->returnJson(1, '删除失败');
    }

    public function zan(Request $request)
    {
        $data = $request->all();
        $data['user_id'] = (int)request()->user()->id;

        $data['status'] = (int)request('status','');
        $data['video_id'] = (int)request('video_id','');
        $data['comment_id'] = (int)request('comment_id','');
        $where['user_id'] = $data['user_id'];
        $where['video_id'] = $data['video_id'];
        $where['comment_id'] = $data['comment_id'];
        try {
            $zanModel = new Praises();
            $find = $zanModel->where($where)->first();
            if(!$find){
                $zanModel->create($data);
            }else{
                $find->update($data);
            }
            if($data['status'] == 1){
                return $this->returnJson(0, '成功点赞');
            }
            return $this->returnJson(0, '成功取消赞');
        } catch (RequestException $exception) {
            Log::error($exception);
            return $this->returnJson(1, '评论失败');
        }
    }
}